package com.mynetwork.gcm;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import com.google.android.gcm.GCMBaseIntentService;
import com.mynetwork.data.NetworkManager;
import com.mynetwork.data.OnNetworkListener;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;


/**
 * @brief [설정] gcm 푸시 알림 서비스 클래스
 * @author PiaJang
 */
public class GCMIntentService extends GCMBaseIntentService{

	public GCMIntentService() {
		super(GCMManager.PROJECT_ID);
	}
	
	@Override
	protected void onError(Context ctx, String arg1) {
		Log.e("gcm_test","gcm error : "+ arg1);		
	}

	@Override
	protected void onMessage(Context ctx, Intent intent) {
		//메시지 전송 받은 후 동작 정의..
		String strmsg = intent.getExtras().getString("message");
		String strTitle = intent.getExtras().getString("title");
		String strsubTitle = intent.getExtras().getString("subtitle");
		int id = intent.getExtras().getInt("idx");
		try {
			strmsg = URLDecoder.decode(strmsg, "UTF-8");
			strTitle =  URLDecoder.decode(strTitle, "UTF-8");
			strsubTitle =  URLDecoder.decode(strsubTitle, "UTF-8");
			
			//상단 노티 생성
			SendNotification.makeNoti(ctx, strTitle, strsubTitle, strmsg, id);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		Log.e("gcm_test","gcm message : " +strmsg);
	}

	@Override
	protected void onRegistered(Context ctx, String key) {
		Log.e("gcm_test","gcm 등록 ID : "+key);
		
		GCMManager.getInstance().setGCMStatus(key, true, mOnDownloadListener);
		GCMManager.getInstance().isRegistration = true;
	}

	@Override
	protected void onUnregistered(Context ctx, String key) {
		Log.e("gcm_test","gcm 등록해제 ID : "+key);
		
		GCMManager.getInstance().setGCMStatus(key, false, mOnDownloadListener);
		GCMManager.getInstance().isRegistration = false;
	}
	
	private OnNetworkListener mOnDownloadListener = new OnNetworkListener() {
		@Override
		public void onSuccess(NetworkManager mgr) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onError(NetworkManager mgr) {
			// TODO Auto-generated method stub
			
		}

	};
	
}
